from rest_framework import serializers

from goods.models import GoodsCategory, Goods, GoodsTags, GoodsImage


# 分类
class CategorySerializer3(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ('id', 'category_name', 'category_code', 'category_img')


class CategorySerializer2(serializers.ModelSerializer):
    sub_cat = CategorySerializer3(many=True)

    class Meta:
        model = GoodsCategory
        fields = ('id', 'category_name', 'category_code', 'sub_cat')


class CategorySerializer(serializers.ModelSerializer):
    sub_cat = CategorySerializer2(many=True)

    class Meta:
        model = GoodsCategory
        fields = ('id', 'category_name', 'category_code', 'sub_cat')


# 商品
class GoodsTagsSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsTags
        fields = ('id', 'tag')


class GoodsImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsImage
        fields = ('image_id', 'image_path')


class GoodListSerializer(serializers.ModelSerializer):
    tags = GoodsTagsSerializer(many=True, read_only=True)

    class Meta:
        model = Goods
        fields = (
            'id', 'good_front_image', 'good_name', 'is_hot', 'is_new', 'market_price',
            'ship_free', 'shop_price', 'sold_num', 'tags')


class GoodSerializer(serializers.ModelSerializer):
    images = GoodsImageSerializer(many=True, read_only=True)

    class Meta:
        model = Goods
        fields = (
            'id', 'good_front_image', 'good_name', 'good_brief', 'shop_price', 'market_price', 'stock_num', 'sold_num',
            'fav_num', 'images')


class OrderCardGoodsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Goods
        fields = ('id', 'good_front_image')
